package cn.edu.ldu.modules.controller;

import cn.edu.ldu.http.rqtUtils;
import cn.edu.ldu.modules.entity.ChatMemberEntity;
import cn.edu.ldu.modules.entity.UserAccountEntity;
import cn.edu.ldu.util.Consts;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class ChatMemberController {

    public static List<UserAccountEntity> getChatMemberAccount(Long groupId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getMemberAccount).append("/").append(groupId), token);

        if(jsonObject.getString("msg").equals("success")){
            return  JSONObject.parseArray(jsonObject.getString("membersAccount"), UserAccountEntity.class);
        }

        return null;

    }
    public static List<ChatMemberEntity> getChatMember(Long groupId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getMemberList).append("/").append(groupId), token);

        if(jsonObject.getString("msg").equals("success")){
            return  JSONObject.parseArray(jsonObject.getString("chatMembers"), ChatMemberEntity.class);
        }

        return null;

    }

    public static String addChatGroup(ChatMemberEntity chatMemberEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(chatMemberEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.addGroupChat), jsonString, token);

        return JSONObject.parseObject(responseResult).getString("msg");

    }

}
